
import 'dart:typed_data';

import 'package:flutter_ns_ble/flutter_ns_ble.dart';

extension NumberBtyeExt on int {
  List<int> toUint8() {
    final byteData = ByteData(4);
    byteData.setInt32(0, this);
    return [byteData.getUint8(0)];
  }

  List<int> toInt8() {
    final byteData = ByteData(4);
    byteData.setInt32(0, this);
    return [byteData.getInt8(0)];
  }

List<int> toUint16({Endian endian = Endian.little}) {
    final byteData = ByteData(4);
    byteData.setInt32(0, this, endian);
    return byteData.buffer.asUint8List(0, 2);
  }

  List<int> toInt16({Endian endian = Endian.little}) {
    final byteData = ByteData(4);
    byteData.setInt32(0, this, endian);
    return byteData.buffer.asInt8List(0, 2);
  }

  List<int> toUint24({Endian endian = Endian.little}) {
    final byteData = ByteData(4);
    byteData.setInt32(0, this, endian);
    return byteData.buffer.asUint8List(0, 3);
  }

  List<int> toInt24({Endian endian = Endian.little}) {
    final byteData = ByteData(4);
    byteData.setInt32(0, this, endian);
    return byteData.buffer.asInt8List(0, 3);
  }

  List<int> toUint32({Endian endian = Endian.little}) {
    final byteData = ByteData(4);
    byteData.setInt32(0, this, endian);
    return byteData.buffer.asUint8List();
  }

  List<int> toInt32({Endian endian = Endian.little}) {
    final byteData = ByteData(4);
    byteData.setInt32(0, this, endian);
    return byteData.buffer.asInt8List();
  }
}

extension ListIntHex on List<int> {
  int sum() {
    return fold(0, (a, b) => a + b);
  }

  String toHex({String separator = ""}) {
    return map(
      (n) => (n & 0xFF).toRadixString(16).padLeft(2, '0'),
    ).join(separator);
  }

  ByteData _byteData({int byteOffset = 0, int? end, List<int> pad = const []}) {
    return Uint8List.fromList([
      ...sublist(byteOffset, end),
      ...pad,
    ]).buffer.asByteData();
  }

  int getUint8({int byteOffset = 0}) {
    assert(sublist(byteOffset).length >= 1, "最少要1个bit");
    return _byteData().getUint8(byteOffset);
  }

  int getInt8({int byteOffset = 0}) {
    assert(sublist(byteOffset).length >= 1, "最少要1个bit");
    return _byteData().getInt8(byteOffset);
  }

  int getUint16({int byteOffset = 0, Endian endian = Endian.little}) {
    assert(sublist(byteOffset).length >= 2, "最少要2个bit");
    return _byteData().getUint16(byteOffset, endian);
  }

  int getInt16({int byteOffset = 0, Endian endian = Endian.little}) {
    assert(sublist(byteOffset).length >= 2, "最少要2个bit");
    return _byteData().getInt16(byteOffset, endian);
  }

  int getUint24({int byteOffset = 0, Endian endian = Endian.little}) {
    assert(sublist(byteOffset).length >= 3, "最少要3个bit");
    return _byteData(
      byteOffset: byteOffset,
      end: byteOffset + 3,
      pad: [0],
    ).getUint32(byteOffset, endian);
  }

  int getInt24({int byteOffset = 0, Endian endian = Endian.little}) {
    assert(sublist(byteOffset).length >= 3, "最少要3个bit");
    return _byteData(
      byteOffset: byteOffset,
      end: byteOffset + 3,
      pad: [0],
    ).getInt32(byteOffset, endian);
  }

  int getUint32({int byteOffset = 0, Endian endian = Endian.little}) {
    assert(sublist(byteOffset).length >= 4, "最少要4个bit");
    return _byteData().getUint32(byteOffset, endian);
  }

  int getInt32({int byteOffset = 0, Endian endian = Endian.little}) {
    assert(sublist(byteOffset).length >= 4, "最少要4个bit");
    return _byteData().getInt32(byteOffset, endian);
  }
}

extension CMDValue on CMD{
  int get value{
    switch (this) {
      
      case CMD.magicCode:
                return 0xbc;
            case CMD.info:
                return 0x10;
            case CMD.imei:
                return 0x20;
            case CMD.imsi:
                return 0x21;
            case CMD.measTag:
                return 0x22;
            case CMD.measFlash:
                return 0x23;
            case CMD.measStatus:
                return 0x24;
            case CMD.measCount:
                return 0x25;
            case CMD.measSN:
                return 0x26;
            case CMD.measFlashCount:
                return 0x27;
            case CMD.measIndex:
                return 0x28;
            case CMD.rGSTSet:
                return 0x28;
            case CMD.rGSTGet:
                return 0x29;
            case CMD.ecgCtl:
                return 0x30;
            case CMD.ecgData:
                return 0x31;
            case CMD.ecgStatus:
                return 0x32;
            case CMD.dfuInit:
                return 0x40;
            case CMD.dfuPacket:
                return 0x41;
            case CMD.dfuCheck:
                return 0x42;
            case CMD.dfuEnd:
                return 0x43;
            case CMD.unknow:
                return 0x00;
            case CMD.writeFactSn:
                return 0x55;
            case CMD.ios:
                return 0x2a;
            case CMD.android:
                return 0x2b;
            case CMD.stopSync:
                return 0x2c;
            case CMD.timeCache:
                return 0x2d;
            
    }
  }
}